๐Ÿ‘ฉ๐Ÿฟโ€๐ŸฆฐBasic Socket.io ํŠœํ† ๋ฆฌ์–ผ with Mysql (1)

๐Ÿ‘ฆ๐ŸปBasic Socket.io

๋‚ด๊ฐ€ ์ƒ๊ฐํ•˜๋Š” ํ•œ ์ค„ ์š”์•ฝ : on ๊ณผ emit ์˜ ํ‹ฐํ‚ค ํƒ€์นด ์ด๋‹ค.

Tiqui-Taca barca ?

์ € ์‹œ์ฆŒ์˜ ๋ฐ”๋ฅด์…€๋กœ๋‚˜๋Š” ๋ญ.. ์—ญ๋Œ€๊ธ‰์ด๊ตฌ๋‚˜.. ๐Ÿ˜ฑ

์—ฌํŠผ ๊ฐ์„คํ•˜๊ณ  ํŠœํ† ๋ฆฌ์–ผ ์ˆœ์„œ๋Œ€๋กœ ๋”ฐ๋ผํ•ด ๋ณด๋ฉด socket.io ์˜ ์ „๋ถ€๋Š” ์•„๋‹ ์ง€๋ผ๋„ ์•„์ฃผ ๋ฏธ์„ธํ•œ ๊ธฐ์ดˆ ์ •๋„๋Š” ์•Œ ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ ๊ธฐ๋Œ€ํ•˜๋ฉฐ (ํŽ˜์–ด๋‹˜? ๋ณด๊ตฌ๊ณ„์‹œ์ง€์œ ?๐Ÿ˜‚) ๊ฐ€๋ณด์ฆˆ์•„!

1. Create empty directory

์ผ๋‹จ ๋นˆ ๋””๋ ‰ํ† ๋ฆฌ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ 

2. Create file โ€œserver.jsโ€

์—๋””ํ„ฐ๋ฅผ ์—ด๋“  ํ•ด์„œ server.js ๋ฅผ ์ƒ์„ฑ ์ดํ›„ ์•„๋ฌด ์ฝ˜์†”๋กœ๊ทธ๋‚˜ ๊ฐ™๋‹ค ์ฐ์–ด ๋†“๊ณ 

3. node server.js ๋กœ ํ™•์ธํ•˜๊ธฐ

4. Install necessary modules

์ ์ ˆํ•œ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค๊ณ  ๊ฑฐ๊ธฐ์—๋‹ค๊ฐ€

npm init -y ๋กœ package.json ์ƒ์„ฑํ•˜๊ธฐ

npm install express http cors nodemon (package.json ์—์„œ nodemon ์œผ๋กœ start ์‹œ ์ปค๋งจ๋“œ๋ฅผ ์ง€์ •ํ•ด์ค€๋‹ค)

5. server.js ์ž‘์„ฑํ•˜๊ธฐ

const express = require('express')
const cors = require('cors')

// ? create instance of express
const app = express()

// ? allows all CORS
app.use(cors())

// ? use http with instance of express
const http = require('http').createServer(app)

const port = 3000

// ? start the server

http.listen(port, () => {
  console.log(`Listening to port ${port}`)
})

๋”์ด์ƒ์˜ ์ž์„ธํ•œ ์„ค๋ช…์€ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค.

6. nodemon server.js ๋กœ ํ™•์ธํ•ด ๋ณผ ๊ฒƒ, ์„œ๋ฒ„ ์‹œ์ž‘์ด ๋˜๋Š”๊ฐ€?

๋ธŒ๋ผ์šฐ์ € ์ƒ์—์„œ localhost:3000 ์€ Cannot GET / ์ด๋ผ๋Š” ๋ฌธ๊ตฌ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

7. Create a simple GET request (in server.js)

app.get('/', (req, res) => {
  res.send('Simple GET request!๐Ÿ')
})

์ด์ œ localhost:3000 ์— ์ ‘์†ํ•˜๋ฉด ์‘๋‹ต์œผ๋กœ ๋ฐ›์€ ๋ฌธ๊ตฌ๊ฐ€ ์ถœ๋ ฅ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

1

8. Integrate Socket

socket.io ๋ฅผ ์„ค์น˜ํ•œ๋‹ค. (for serverside)

npm install socket.io

๊ณต์‹๋ฌธ์„œ์—๋„ ๋‚˜์™€ ์žˆ์ง€๋งŒ socket.io ๋ฅผ ์“ธ ๋•Œ๋Š” http ๋ชจ๋“ˆ๊ณผ express ๋ชจ๋“ˆ์„ ํ˜ผํ•ฉ ์‚ฌ์šฉํ•œ๋‹ค.

server.js ๋‚ด์— ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ http ๋ณ€์ˆ˜ ์„ ์–ธํ•œ ๋ถ€๋ถ„ ์•„๋ž˜์—๋‹ค ์ž‘์„ฑํ•œ๋‹ค.

// create socket instance with http
const io = require('socket.io')(http)

// add listeners for new connection
// ์‹ค์‹œ๊ฐ„ chat (socket) ์„ ์‚ฌ์šฉํ•˜๋Š” ์‚ฌ์šฉ์ž๋ฅผ ์œ„ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ, socket
io.on('connection', socket => {
  // this is socket for each user
  console.log('User connected', socket.id)
  // ์ด๋”ฐ ํ„ฐ๋ฏธ๋„์—์„œ ๋ณด๊ฒ ์ง€๋งŒ ๋งค๋ฒˆ ์š”์ฒญ์‹œ๋งˆ๋‹ค socket.id ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ํ™•์ธ๋œ๋‹ค.
})

2

9. make index.html on same root directory

๊ฐ™์€ ๋ฃจํŠธ์— index.html ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.

<!-- socket.io-client ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ฒŒ ํ•œ๋‹ค. body ํƒœ๊ทธ ๋งˆ์ง€๋ง‰์— ์ž‘์„ฑํ•ด์„œ -->
<!-- io ๋ชจ๋“ˆ์„ ์ „์—ญ์— ๋…ธ์ถœ์‹œํ‚ค๋Š” ์˜๋ฏธ -->
<!-- io ๋ชจ๋“ˆ ๋‚ด ์ธ์ž๋Š” ๋ฐฑ์—”๋“œ์˜ ์ฃผ์†Œ-->

<script src="/socket.io/socket.io.js"></script>

<script>
  const socket = io('http://localhost:3000')
</script>

์ด๋ ‡๊ฒŒ ์ž‘์„ฑํ•˜๊ณ 

server.js ๋‚ด ์ฝ”๋“œ์˜ simple get request ๋ถ€๋ถ„์„ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค.

// ? Create a Simple GET request
app.get('/', (req, res) => {
  res.sendFile(__dirname + '/index.html')
})

ํด๋ผ์ด์–ธํŠธ์™€ ์—ฐ๊ฒฐ์‹œํ‚ค๊ธฐ ์œ„ํ•จ์ด๋‹ค. ๋ฆฌ์•กํŠธ์—์„œ๋Š” ์ด๋ ‡๊ฒŒ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค. ๋ˆ ์›Œ๋ฆฌ

์ดํ›„ ๋ธŒ๋ผ์šฐ์ € (localhost:3000) ์„ ์ƒˆ๋กœ๊ณ ์นจ ํ•˜๋ฉด์„œ nodemon ์œผ๋กœ ์‹คํ–‰ํ•œ ํ„ฐ๋ฏธ๋„์„ ์œ ์‹ฌํžˆ ์‚ดํŽด๋ณด์ž.

โ€œUser connected abcdefb123456cdefgโ€ ๋“ฑ์˜ ๋ฌธ๊ตฌ๊ฐ€ ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. ๋’ค์˜ ์ด์ƒํ•œ ๋ฌธ์ž๊ฐ€ ๋ฐ”๋กœ socket.id ์ด๋‹ค.

3

10. Send message

index.html ๋ถ€๋ถ„์—์„œ ์ž‘์„ฑํ•œ ์ฝ”๋“œ ์—์„œ ๋‘๋ฒˆ์งธ script ํƒœ๊ทธ ๋ถ€๋ถ„ ์•„๋ž˜์— socket.emit ์ดํ•˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.

<script>
  const socket = io('http://localhost:3000')
  socket.emit('new_message', 'Hello, Socket.io!๐Ÿค')
</script>

sending message from client, also server should listen from each client via itโ€™s socket.

์„œ๋ฒ„์—์„œ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›์•„์ฃผ๊ธฐ ์œ„ํ•ด์„œ๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋“ค์–ด์•ผ ํ•œ๋‹ค.

ํ˜„์žฌ๋Š” ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ new_message ๋ผ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ ๋ณ€์ˆ˜๋ฅผ ํ†ตํ•ด ๋ฉ”์‹œ์ง€๋ฅผ ์„œ๋ฒ„๋กœ ์ „๋‹ฌํ•˜๊ณ ์ž ํ•œ๋‹ค.

๋ฐ”๋กœ ์ด ๋•Œ ์“ฐ๋Š” ์ฝ”๋“œ๊ฐ€ emit ์ด๋‹ค! (๋ฐœ์Œ์— ์œ ์˜ํ•  ๊ฒƒ)

11. ์„œ๋ฒ„๋Š” ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋˜์ง„ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์„ ์ค€๋น„๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค. (server.js)

ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„๋กœ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋ƒˆ์œผ๋‹ˆ, ์„œ๋ฒ„๋Š” ์ด ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ›์„ ์ค€๋น„๋ฅผ ํ•ด์•ผ ํ•œ๋‹ค.

server.js ๋‚ด ์ฝ”๋“œ์—์„œ

io.on ์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ๊ธฐ์กด ์ฝ”๋“œ ๋‚ด console.log ์•„๋ž˜์— socket.on ์ดํ•˜์˜ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด ๋ณธ๋‹ค.

io.on(โ€œconnectionโ€, (socket) => {
  // this is socket for each user
  console.log("User connected", socket.id);
  socket.on("new_message", (data) => {
      console.log("Client says", data);
    })
});

์„œ๋ฒ„์—์„œ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›์•„์ฃผ๊ธฐ ์œ„ํ•ด์„œ๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋“ค์–ด์•ผ ํ•œ๋‹ค.

๊ทธ ๋•Œ ์“ฐ๋Š” ์ฝ”๋“œ๊ฐ€ ๋ฐ”๋กœ on ์ด๋‹ค!

ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์€ socket ์„ ๊ฐ€์ ธ์™€์„œ, ํด๋ผ์ด์–ธํŠธ์—์„œ emit ์œผ๋กœ ์ž‘์„ฑํ•œ newmessage ์˜ ๋ณ€์ˆ˜๋ฅผ ์ผ์น˜์‹œ์ผœ ์ฒซ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋„ฃ๋Š”๋‹ค. (newmessage)

๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์˜ data ๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ ๋‘๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์— ๋‹ด๊ธด ๊ฐ’์„ ์˜๋ฏธํ•œ๋‹ค.

์ด์ œ ํด๋ผ์ด์–ธํŠธ (๋ธŒ๋ผ์šฐ์ €) ๋ฅผ ์ƒˆ๋กœ๊ณ ์นจ ํ•ด ์ฃผ๊ณ  nodemon ์œผ๋กœ ์—ด์–ด๋†“์€ ํ„ฐ๋ฏธ๋„์„ ๋ณด๋ฉด,

โ€œClient says Hello, Socket.io!๐Ÿคโ€ ๋ผ๋Š” ๋ฌธ๊ตฌ๋ฅผ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค.

์ฆ‰, ํด๋ผ์ด์–ธํŠธ์˜ ๋ฉ”์‹œ์ง€๋ฅผ ์„œ๋ฒ„๊ฐ€ ๋“ฃ๊ฒŒ ๋œ ๊ฒƒ์ด๋‹ค (ํ•˜๋Š๋‹˜ ๋“ค์œผ์…จ๋‚˜์š”? ๊ฐ™์€ ๋Š๋‚Œ)

4

12. ์„œ๋ฒ„์—์„œ๋„ ๋ฉ”์‹œ์ง€๋ฅผ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์ „๋‹ฌํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

ํ˜„์žฌ ์ž‘์„ฑ๋œ ์ฝ”๋“œ์—์„œ,

console.log ์•„๋ž˜์— io.emit ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•œ๋‹ค.

io.on('connection', socket => {
  // this is socket for each user
  console.log('User connected', socket.id) // ์ด๋”ฐ ํ„ฐ๋ฏธ๋„์—์„œ ๋ณด๊ฒ ์ง€๋งŒ ๋งค๋ฒˆ ์š”์ฒญ์‹œ๋งˆ๋‹ค socket.id ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ํ™•์ธ๋œ๋‹ค.
  socket.on('new_message', data => {
    console.log('Client says', data)

    // ? ์˜ค์ผ€์ด ๊ทธ ๋‹ค์Œ์€ socket.io ๋กœ ์—ฐ๊ฒฐ๋œ ๋ชจ๋“  ํด๋ผ์ด์–ธํŠธ๋“ค์—๊ฒŒ ์„œ๋ฒ„๊ฐ€ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๊ฒŒ ํ•  ๊ฒƒ์ด๋‹ค.

    io.emit('new_message', 'Hello, Client๐Ÿ˜Š')
  })
})

์ž ์ด์ œ ์ด๊ฑธ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋“ฃ๊ฒŒ ํ•˜๋ ค๋ฉด?

๋‹ค์‹œ ํด๋ผ์ด์–ธํŠธ (index.html) ๋กœ ๋Œ์•„๊ฐ€์„œ .on ์œผ๋กœ ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ์˜ ์‘๋‹ต์„ ๋“ฃ๊ฒŒ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•œ๋‹ค. ๊ฐ€์ž!

์„œ๋‘์— ์–ธ๊ธ‰ํ–ˆ๋˜ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๊ธฐ (emit), ๋ฉ”์‹œ์ง€ ์š”์ฒญ์„ ๋“ฃ๊ธฐ (on) ๋ผ๋Š” ๋‚ด์šฉ์ด emit ๊ณผ on ์˜ ํ‹ฐํ‚ค ํƒ€์นด ๋ผ๋Š” ๋ง์ด ์ดํ•ด๊ฐ€ ์ข€ ๋ ๊นŒ์š”?๐Ÿ˜Š

13. ํด๋ผ์ด์–ธํŠธ์—์„œ ์„œ๋ฒ„์˜ ์‘๋‹ต์„ ๋ฐ›๊ฒŒ ํ•˜๊ธฐ. (index.html)

socket.on ์„ ํ†ตํ•ด ์„œ๋ฒ„์—์„œ ์ „๋‹ฌํ•ด์ฃผ๋Š” ์‘๋‹ต์„ ๋ฐ›๋Š”๋‹ค. ์ž‘์„ฑํ•ด๋ณด์ž.

<script>
  const socket = io('http://localhost:3000')

  socket.emit('new_message', 'Hello, Socket.io!๐Ÿค')

  // client will listen from server
  socket.on('new_message', data => {
    // localhost:3000 ํŽ˜์ด์ง€ ๋‚ด ๊ฐœ๋ฐœ์ž ๋„๊ตฌ ์ฐฝ์—์„œ ํ™•์ธํ•ด ๋ณผ ๊ฒƒ
    console.log('Server says', data)
  })
</script>

5

14. Send message from form and display in list

index.html ์—์„œ ๋‘ ์Šคํฌ๋ฆฝํŠธ ํƒœ๊ทธ ์‚ฌ์ด์— form ํƒœ๊ทธ๋กœ ์ž…๋ ฅ์ฐฝ ๋งŒ๋“ค์–ด ๋†“์ž.

<form onsubmit="return sendMessage()">
  <input id="message" placeholder="Enter message" />
  <input type="submit" value="Send" />
</form>

๋„ˆ๋ฌด ๊ธธ๋ฉด ์•ˆ๋˜๋ฏ€๋กœ 2๋ถ€์—์„œ ๋ต™์‹œ๋‹ค.๐Ÿคก


Written by@[DotoriMook]
ํ”„๋ก ํŠธ์—”๋“œ ์ฃผ๋‹ˆ์–ด ๊ฐœ๋ฐœ์ž ๋„ํ† ๋ฆฌ๋ฌต์˜ ๊ธฐ์ˆ ๊ฐœ๋ฐœ ๋ธ”๋กœ๊ทธ ์ž…๋‹ˆ๋‹ค.

GitHubMediumTwitterFacebook